package com.cskaoyan.service.mall;

import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketBrand;
import com.cskaoyan.bean.po.MarketBrandExample;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProducersServicelmpl implements ProducersService {

    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Override
    public CommonData<MarketBrand> queryProducersBrandList(BasePageInfo basePageInfo, String id, String name) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name.trim() + "%");
        }
        if (!StringUtil.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id.trim()));
        }
        marketBrandExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);

        PageInfo<MarketBrand> marketBrandPageInfo = new PageInfo<>(marketBrands);

        return CommonData.data(marketBrandPageInfo);
    }

    @Override
    public MarketBrand createProducersBrand(MarketBrand marketBrand) {
        marketBrand.setAddTime(new Date());
        marketBrand.setUpdateTime(new Date());
        int code = marketBrandMapper.insertSelective(marketBrand);
        if (code == 0) {
            return null;
        }
        MarketBrand selectByPrimaryKey = marketBrandMapper.selectByPrimaryKey(marketBrand.getId());
        return selectByPrimaryKey;
    }

    @Override
    public int deleteProducersBrand(Integer id) {
        int code = marketBrandMapper.deleteByPrimaryKey(id);
        return code;
    }

    @Override
    public MarketBrand updateProducersBrand(MarketBrand marketBrand) {
        marketBrand.setUpdateTime(new Date());
        int code = marketBrandMapper.updateByPrimaryKeySelective(marketBrand);
        if (code == 0) {
            return null;
        }
        MarketBrand marketBrand1 = marketBrandMapper.selectByPrimaryKey(marketBrand.getId());
        return marketBrand1;
    }
}
